using System;
using System.Collections;
using NHibernate;
using YouLink.Core.Model;

namespace YouLinkWebService
{
    public partial class _Default : System.Web.UI.Page
    {
        Site s;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                IList siteList;
                ISessionFactory factory = new NHibernate.Cfg.Configuration().Configure().BuildSessionFactory();
                using (ISession session = factory.OpenSession())
                {
                    ICriteria sc = session.CreateCriteria(typeof(Site));
                    siteList = sc.List();
                    session.Close();
                }
                factory.Close();
                s = (Site)siteList[0];
                Label1.Text = s.Id.ToString();
                TextBox1.Text = s.Name;
                
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            ISessionFactory factory = new NHibernate.Cfg.Configuration().Configure().BuildSessionFactory();
            using (ISession session = factory.OpenSession())
            {
                s = (Site)session.Load(typeof(Site), int.Parse(Label1.Text)); 
                s.Name = TextBox1.Text;
                session.SaveOrUpdate(s);
                session.Flush();
                session.Close();
            }
            factory.Close();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            ISessionFactory factory = new NHibernate.Cfg.Configuration().Configure().BuildSessionFactory();
            using (ISession session = factory.OpenSession())
            {
                s = (Site)session.Load(typeof(Site), int.Parse(Label1.Text));
                
                session.Delete(s);
                session.Flush();
                session.Close();
            }
            factory.Close();
        }
    }
}
